package com.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.robot.common.api.CurrentUserSupport;
import com.robot.common.enums.IsDeleteEnum;
import com.robot.dao.entity.Robot;
import com.robot.dao.feigndto.context.RequestContext;
import com.robot.dao.mapper.CategoryMapper;
import com.robot.dao.mapper.RobotMapper;
import com.robot.dao.mapper.UserMapper;
import com.robot.dao.dto.CategoryDTO;
import com.robot.dao.dto.IDDTO;
import com.robot.dao.entity.Category;


import com.robot.dao.vo.UserInfoVO;
import com.robot.repository.RobotRepository;
import com.robot.service.CategoryService;
import com.robot.dao.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private CurrentUserSupport currentUserSupport;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private RobotRepository robotRepository;

    /**
     * 根据用户ID查询类目列表
     * @param iddto
     * @return
     */
    public List<CategoryVO> queryByEnterpriseId(IDDTO iddto
    ) {
        // 查询类目列表：管理员建的机器人列表、自己建的机器人列表、自己所在公司建的机器人列表。查到之后，get类目。
        RequestContext.UserAccountInfoCtx userAccountInfo= RequestContext.getUserAccountInfo();
        String currentUserComponyCode= userAccountInfo.getComponyCode();//
        Long currentUserId= userAccountInfo.getUserID();//
        List<Long> iDList = userService.getManagerUserIDList();
        iDList.add(currentUserId);
        List<String> resName= robotRepository.getDistinctCategoryNamesByUserOrCompany(iDList,currentUserComponyCode);

        List<CategoryVO> categoryVOS = new ArrayList<>();
        for (String category : resName) {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setCategoryId("0");
            categoryVO.setCategoryName(category);
            categoryVOS.add(categoryVO);
        }
        return categoryVOS;
    }



    /**
     * 创建新类目
     * @param categoryDTO
     * @return
     */
    public CategoryVO create(CategoryDTO categoryDTO) {
        //由于同一用户下类目名称唯一，所以先查存在不存在
        List<Category> categories = categoryMapper.queryByEnterpriseId(categoryDTO.getCompanyCode());
        for (Category category : categories) {
            //如果已经存在的话
            if (category.getCategoryName().equals(categoryDTO.getCategoryName())){
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                categoryVO.setCategoryId(String.valueOf(category.getId()));

                return categoryVO;
            }
        }

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUserId(Long.valueOf(categoryDTO.getUserId()));
        category.setCompanyCode(categoryDTO.getCompanyCode());
        category.setIsDelete(IsDeleteEnum.EXIST.getValue());
        categoryMapper.create(category);

        CategoryVO categoryVO = new CategoryVO();
        BeanUtils.copyProperties(category, categoryVO);
        categoryVO.setCategoryId(String.valueOf(category.getId()));
        return categoryVO;
    }
}
